//
//  ShopViewController.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 8/1/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "ShopViewController.h"
#import "KeyObject.h"
#import "ShopObject.h"
#import "ProductObject.h"
//#import "ZBarReaderViewController.h"

@interface ShopViewController ()

@end

@implementation ShopViewController

- (id)initWithStyle:(UITableViewStyle)style items:(NSArray *)array tag:(int)tag;
{
    self = [super initWithStyle:style];
    if(self)
    {
        items = array;
        btntag = tag;
    }
    [self.tableView setSeparatorColor:[UIColor colorWithRed:113/255.0 green:68/255.0 blue:54/255.0 alpha:1]];
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    self.view.backgroundColor=[UIColor colorWithRed:210/255.0 green:189/255.0 blue:149/255.0 alpha:1];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:210/255.0 green:189/255.0 blue:149/255.0 alpha:1];
    cell.textLabel.textColor = [UIColor colorWithRed:43/255.0 green:11/255.0 blue:0/255.0 alpha:1];
    cell.textLabel.adjustsFontSizeToFitWidth=YES;
    if (btntag > 110) {
        KeyObject * key = [items objectAtIndex:indexPath.row];
        cell.textLabel.text = key.keyName;
    }
    else if(btntag ==100){
        cell.textLabel.text = [[items objectAtIndex:indexPath.row]objectForKey:@"name"];
    }
    else if(btntag ==102){
         ShopObject* shop = [items objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",shop.shopID,shop.shopName];
    }
    else if(btntag ==11){
        ProductObject* product = [items objectAtIndex:indexPath.row];
        cell.textLabel.text =[NSString stringWithFormat:@"%@-%@%@",product.pCode,product.pColorCode,product.pColor];
    }

    else
    {
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    }
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (btntag == 10) {
        NSString *str = [NSString stringWithFormat:@"%d",indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"index1" object:str];
    }
    else if(btntag == 11)
    {
        NSString *str = [NSString stringWithFormat:@"%d",indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"coderefresh" object:str];
    }
    else if(btntag == 12)
    {
        NSString *str = [items objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"searchrefresh" object:str];
    }
    else if(btntag == 13)
    {
        NSString *str = [NSString stringWithFormat:@"%d",indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"shoprefresh" object:str];
    }
    else if(btntag == 14)
    {
        NSString *str = [NSString stringWithFormat:@"%d",indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"set" object:str];
    }
    else if(btntag == 100)
    {
        NSDictionary* dict = [items objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshReport" object:dict];
    }
    else if(btntag == 101)
    {
        KeyObject *key = [items objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshnum" object:key];
    }
    else if(btntag == 102)
    {
        ShopObject* shop = [items objectAtIndex:indexPath.row];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        
        if (shop.shopID) {
            [dic setObject:shop.shopID forKey:@"shopid"];
        }
        
        [dic setObject:shop.shopName forKey:@"title"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshshop" object:dic];
    }
    else if(btntag == 103)
    {
       // NSString *str = [items objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"totalrefresh" object:nil];
    }
    else if(btntag == 104)
    {
        // NSString *str = [items objectAtIndex:indexPath.row];
        SubSuitObject *sub = [self.subobjs objectAtIndex:indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"toDetail1" object:sub];
    }
    //报表的过滤条件
    else if(btntag >110&& btntag < 200){
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshnum" object:dic];
        
    }
    //配装的过滤条件
    else if(btntag > 200&&btntag<300){
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"suitFilterRefresh" object:dic];
    }
    else if(btntag > 300)
    {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"suitFilterRefresh1" object:dic];
    }
}

@end
